package com.shiyanlou.upms.system.controller;

import com.shiyanlou.upms.common.utils.PageUtils;
import com.shiyanlou.upms.common.utils.Query;
import com.shiyanlou.upms.common.utils.R;
import com.shiyanlou.upms.system.domain.UpmsPermission;
import com.shiyanlou.upms.system.domain.UpmsRole;
import com.shiyanlou.upms.system.domain.UpmsRolePermission;
import com.shiyanlou.upms.system.service.UpmsRoleService;
import com.shiyanlou.upms.system.service.UpmsUserService;
import com.shiyanlou.upms.system.domain.UpmsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 */
@Controller
@RequestMapping("/role")
public class UpmsRoleController {

    @Autowired
    private UpmsRoleService upmsRoleService;
    @Autowired
    private UpmsUserService upmsUserService;

    @GetMapping("/main")
    String UpmsRole(){
        return "role/main";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<UpmsRole> upmsRoleList = upmsRoleService.list(query);
        int total = upmsRoleService.count(query);
        PageUtils pageUtils = new PageUtils(upmsRoleList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "role/add";
    }

    @GetMapping("/edit/{roleId}")
    String edit(@PathVariable("roleId") Integer roleId, Model model){
        UpmsRole upmsRole = upmsRoleService.selectByPrimaryKey(roleId);
        model.addAttribute("upmsRole", upmsRole);
        return "role/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    public R save(UpmsRole upmsRole){
        if(upmsRoleService.insert(upmsRole)>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/update")
    public R update( UpmsRole upmsRole, Integer[] permissionArr){
        List<UpmsRolePermission> upmsRolePermissionDOList = new ArrayList<UpmsRolePermission>();
        upmsRoleService.update(upmsRole, permissionArr);
        return R.ok();
    }

    @PostMapping( "/remove")
    @ResponseBody
    public R remove(Integer roleId){
        if(upmsRoleService.deleteByPrimaryKey(roleId)>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] roleIds){
        upmsRoleService.batchRemove(roleIds);
        return R.ok();
    }
    /**
     * 获取角色拥有的资源
     */
    @ResponseBody
    @PostMapping("/selectRoleHasPermission/{roleId}")
    public List<UpmsPermission> selectRoleHasPermission(@PathVariable("roleId") Long roleId) {
        List<UpmsPermission> upmsPermissionDOList = new ArrayList<>();
        upmsPermissionDOList = upmsRoleService.selectRoleHasPermissionByRoleId(roleId);
        return upmsPermissionDOList;
    }
    /**
     * 角色授权用户页面
     * @return
     */
    @GetMapping("/authRole/{roleId}")
    public String authRole(@PathVariable("roleId") Integer roleId, Model model) {
        UpmsRole upmsRole = upmsRoleService.selectByPrimaryKey(roleId);
        model.addAttribute("upmsRole", upmsRole);
        return "role/authUser";
    }

    @ResponseBody
    @GetMapping("/selectAuthRoleUser")
    public PageUtils selectAuthRoleUserByRoleId(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<UpmsUser> upmsUserDOList = new ArrayList<>();
        int count = upmsUserService.selectUserCountByRole(query);
        if(count > 0) {
            upmsUserDOList = upmsUserService.selectUserByRole(query);
        }
        PageUtils pageUtils = new PageUtils(upmsUserDOList, count);
        return pageUtils;
    }

    @GetMapping("/selectUser/{roleId}")
    public String selectUser(@PathVariable("roleId") Integer roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return "role/selectUser";
    }

    @ResponseBody
    @GetMapping("/selectRoleByUserId/{userId}")
    public PageUtils list(@PathVariable("userId") Integer userId){

        int total = upmsRoleService.selectRoleCountByUserId(userId);
        List<UpmsRole> upmsRoleList = upmsRoleService.selectRoleByUserId(userId);
        PageUtils pageUtils = new PageUtils(upmsRoleList, total);
        return pageUtils;
    }

    @PostMapping("/addRoleForUser")
    @ResponseBody
    public R addRoleForUser(@RequestParam("roleId") Integer roleId,
                            @RequestParam("ids[]") Integer[] userIds) {
        try {
            int count = upmsRoleService.batchAddUser(roleId, userIds);
            if(count>0) {
                return R.ok("新增" + count + "条记录！");
            }
            return R.error("用户均已存在，无需新增！");
        } catch (RuntimeException e) {
            return R.error();
        }
    }


}
